import wepy from 'wepy'
export default class extends wepy.mixin {
    data = {
        query: '',
        cid: '',
        pagenum: 1,
        pagesize: 10,
        goodsList: [],
        total: 0,
        isover: true,
        // 是否正在加载 解决网速慢同时发生2次请求的bug
        isloading: false
    }

    methods = {
        goGoodsDetail(goods_id) {
            wepy.navigateTo({
                url: '/pages/goods_detail/main?goods_id=' + goods_id
            })
        }
    }

    onLoad(o) {
        console.log(o)
        this.query = o.query || ''
        this.cid = o.cid || ''
        this.getGoodsList()
    }

    async getGoodsList(cb) {
        this.isloading = true
        var { data: res } = await wepy.get('/goods/search', {
            query: this.query,
            cid: this.cid,
            pagenum: this.pagenum,
            pagesize: this.pagesize
        })
        console.log(res)

        if (res.meta.status != 200) {
            return wepy.baseToast()
        }
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
        this.isloading = false
        cb && cb()
        this.$apply()
    }

    onReachBottom() {
        if (this.isloading) {
            return
        }
        // console.log('触底了')
        if (this.pagenum * this.pagesize >= this.total) {
            this.isover = false
            return
        }
        this.pagenum++
            this.getGoodsList()
    }

    onPullDownRefresh() {
        this.pagenum = 1
        this.goodsList = []
        this.total = 0
        this.isloading = false
        this.isover = true
        this.getGoodsList(function() {
            wepy.stopPullDownRefresh
        })
    }
}